<?php

namespace App\Models;

use Illuminate\Auth\Authenticatable;  
use Illuminate\Database\Eloquent\Model;  
use Illuminate\Auth\Passwords\CanResetPassword;  
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;  
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

use App\Models\Images;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract {  
    
    use Authenticatable, CanResetPassword; 

    /**
     * 关联到模型的数据表
     *
     * @var string
     */
    protected $table = 'users';
    
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'nickname', 'phone', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * 获取关联到用户的头像
     */
    public function images()
    {
        return $this->hasOne('App\Models\Images');
    }
    
    /**
     * 设置用户头像地址
     *
     * @param  string  $value
     * @return string
     */
    public function getHeadUrlAttribute()
    {
        $image = $this->headportrait_id;
        $image = Images::where('id',$image)->first();
        $url = '';
        if(!$image){
            return $url;
        }
        $url = url(config('url.images_url')).'/'.$image->filepath.$image->filename;
        return $url;
    }

}
